package com.csdj.questions.controller;


import com.csdj.questions.common.WebResult;
import com.csdj.questions.common.WebResultMsgCode;
import com.csdj.questions.service.AnswerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 回答表 前端控制器
 * 主页搜索框，两个方法都需要调用
 * </p>
 *
 * @author 刘宇
 * @since 2022-03-08
 */
@RestController
@RequestMapping("/answer")
public class AnswerController {
    @Resource
    private AnswerService answerService;

    /**
     * 模糊查询，主页搜索框 调用的时候字段是对应数据库的，不是实体类
     * @name 刘宇
     * @return
     */
    @GetMapping("/selectLike")
    public WebResult selectLike(@RequestParam(value = "quesName",required = false)String quesName,
                                @RequestParam(value = "articleName",required = false)String articleName){
        List<Map<String,Object>> list = answerService.selectLike(quesName,articleName);
        if(list != null){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("selectLike",list);
        }
        return null;
    }

    /**
     * 根据问题id 查询 点赞的次数  搜索框
     * @name 刘宇
     * @param quesId 问题id
     * @return
     */
    @GetMapping("/selectCount")
    public WebResult selectCount(@RequestParam("quesId")String quesId){
        int count = answerService.selectCount(quesId);
        if(count >= 0 ){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("selectCount",count);
        }
        return null;
    }
}
